/*
 * 用户注册界面
 */
package com.cqtczh.qhd.driver.acticity.user;

import httptools.HttpJsonTool;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javabeen.UserInfoBeen;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import cn.jpush.android.api.JPushInterface;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.cqtczh.qhd.driver.AppContent;
import com.cqtczh.qhd.driver.R;
import com.cqtczh.qhd.driver.acticity.ProjectHomeActivity;
import com.cqtczh.qhd.driver.acticity.base.BaseActivity;
import com.cqtczh.qhd.driver.api.GagApi;
import com.cqtczh.qhd.driver.model.LoginUserInfo;
import com.cqtczh.qhd.driver.utils.Base64Util;
import com.cqtczh.qhd.driver.utils.SPUtils;

public class UserLoginActivity extends BaseActivity  {

	private HttpJsonTool http;
	private EditText login_id, login_pwd;
	public static Gson gson = new Gson();
	private Button login_denglu;

private AppContent app;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_user_login);
		// 初始话view和变量
		Ini();
		// 自动登录登录
		AntLogin();
		Button button1 = (Button) findViewById(R.id.login_zhuce);
		button1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				openActivity(UserRegisterActivity.class);
			}
		});
		//Button button2=(Button) findViewById(R.id.open);
		//button2.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View arg0) {
//				// TODO Auto-generated method stub
//				openActivity(ProjectHomeActivity.class);
//			}
//		});
	}

	private void Ini() {
		app=(AppContent) getApplication();
		
		JPushInterface.resumePush(getApplicationContext());
		
		http = new HttpJsonTool(this,true);
		login_id = (EditText) findViewById(R.id.login_id);
		login_pwd = (EditText) findViewById(R.id.login_pwd);
		login_denglu = (Button) findViewById(R.id.login_denglu);
		login_denglu.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				HashMap< String, Object> dataMap=new HashMap<String, Object>();
				String phone=login_id.getText().toString().trim();
				String password=login_pwd.getText().toString().trim();
				if (phone==null||phone.length()<=0) {
					return;
				}
				if (password==null||password.length()<=0) {
					return;
				}
				dataMap.put("phone", phone);
				dataMap.put("password", password);
				Login(dataMap);
			}
		});
		TextView title = (TextView) findViewById(R.id.head_title);
		//title.setText("登录");
		/**
		 * 忘记密码
		 */
		TextView textView=(TextView) findViewById(R.id.forgetPassword);
		textView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
			openActivity(RestPassWordActivity.class);	
			}
		});
	}

	// 自动登录
	private void AntLogin() {
		String username = (String) SPUtils.get(this, "phone", "");
		String password = (String) SPUtils.get(this, "password", "");
		password = Base64Util.fromHex(password);
		if (username.isEmpty() || password.isEmpty()) {
			// 没有保存过的登录的信息

		} else {
			HashMap<String, Object> dataMap = new HashMap<String, Object>();
			dataMap.put("phone", username);
			dataMap.put("password", password);
			Login(dataMap);
		}
	}

	private void Login(final Map<String, Object> dataMap) {
		showLoadingDialog("加载中");
		new ArrayList<LoginUserInfo>();
		Type type = new TypeToken<List<UserInfoBeen>>() {
		}.getType();
		//SeaveUserInfo(dataMap);
		http.SetOnLister(new Handler() {
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if (msg.what == 1) {
					//app.userInfoBeen
					dismissLoadingDialog();
					List<UserInfoBeen>d=(List<UserInfoBeen>) msg.obj;
					app.userInfoBeen=d.get(0);
					app.userId=d.get(0).getCarownerid()+"";
					SeaveUserInfo(dataMap);				
				}else {
					Toast.makeText(UserLoginActivity.this, (String)msg.obj, Toast.LENGTH_LONG).show();
					dismissLoadingDialog();
				}
			}
		});
		http.StartWork(dataMap, GagApi.LOGIN, type, null);
	}

	// 保存用户密码和id
	private void SeaveUserInfo(Map<String, Object> dataMap) {		
		SPUtils.put(UserLoginActivity.this, "phone",
				dataMap.get("phone"));
		SPUtils.put(UserLoginActivity.this, "password",
				Base64Util.toHex((String) dataMap.get("password")));
		openActivity(ProjectHomeActivity.class);
		this.finish();
	}

//	@Override
//	public void onClick(View arg0) {
//		// TODO Auto-generated method stub
//		String userName = login_id.getText().toString();
//		String userPassword = login_pwd.getText().toString();
//		if ((userName != null && userName.length() > 0)
//				&& (userPassword != null && userPassword.length() > 0)) {
//			HashMap<String, Object> datamap = new HashMap<String, Object>();
//			datamap.put("phone", userName);
//			datamap.put("password", userPassword);
//
//			Login(datamap);
//		} else {
//			Toast.makeText(UserLoginActivity.this, "请输入用户名和密码！",
//					Toast.LENGTH_LONG).show();
//		}
//	}

}
